package com.apex.lottery.controller;

import com.apex.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.apex.lottery.common.exception.ControllerException;
import com.apex.lottery.common.result.CommonResult;
import com.apex.lottery.common.utils.JacksonUtil;
import com.apex.lottery.controller.param.CreateActivityParam;
import com.apex.lottery.controller.param.PageParam;
import com.apex.lottery.controller.result.CreateActivityResult;
import com.apex.lottery.controller.result.FindActivityListResult;
import com.apex.lottery.controller.result.GetActivityDetailResult;
import com.apex.lottery.service.ActivityService;
import com.apex.lottery.service.dto.ActivityDetailDTO;
import com.apex.lottery.service.dto.CreateActivityDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.apex.lottery.controller.converter.ActivityConverter.*;
import static com.apex.lottery.controller.converter.ActivityConverter.convertToGetActivityDetailResult;
import static com.apex.lottery.controller.converter.ActivityConverter.convertToGetActivityDetailResult;
import static com.apex.lottery.controller.converter.ActivityConverter.convertToGetActivityDetailResult;

@RestController
public class ActivityController {

    private static final Logger logger = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;

    /**
     * 创建活动
     *
     * @param param
     * @return
     */
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@Validated @RequestBody CreateActivityParam param) {
        logger.info("createActivity CreateActivityParam:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(convertToCreateActivityResult(activityService.createActivity(param)));
    }

    /**
     * 获取活动列表
     *
     * @param param
     * @return
     */
    @GetMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {
        logger.info("findActivityList PageParam:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(convertToFindActivityListResult(activityService.findActivityList(param)));
    }

    @GetMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail (Long activityId){
        logger.info("getActivityDetail activityId:{}", activityId);
        ActivityDetailDTO activityDetailDTO =  activityService.getActivityDetail(activityId);
        return CommonResult.success(convertToGetActivityDetailResult(activityDetailDTO));
    }


}
